import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { AjaxResult } from '@/api/model';
import { SettingInputDto, SettingOutputDto, SiteDescription } from './model';

/** 管理-系统设置枚举 */
enum Api {
  /** 读取设置 */
  Read = '/api/admin/settings/read',
  /** 保存设置 */
  Update = '/api/admin/settings/update',
  /** 获取应用标题 */
  GetAppTitle = '/api/admin/settings/getAppTitle',
  /** 设置应用标题 */
  SetAppTitle = '/api/admin/settings/setAppTitle',
  /** 删除应用标题 */
  DeleteAppTitle = '/api/admin/settings/deleteAppTitle',
  /** 获取应用描述 */
  GetAppDescription = '/api/admin/settings/getAppDescription',
  /** 设置应用描述 */
  SetAppDescription = '/api/admin/settings/setAppDescription',
  /** 删除应用描述 */
  DeleteAppDescription = '/api/admin/settings/deleteAppDescription',
}

//#region 读取设置
/**
 * 读取设置
 * @param root 设置根节点，如投票设置为Votes
 * @param mode 错误消息模型
 */
export function readSettings(root: string, mode: ErrorMessageMode = 'message') {
  return httpClient.get<SettingOutputDto>(
    {
      url: Api.Read,
      params: { root: root },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 保存设置
/**
 * 保存设置
 * @param dto 设置信息
 * @param mode 错误消息模型
 */
export function updateSettings(dto: SettingInputDto, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Update,
      params: dto,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 获取应用标题
/**
 * 获取应用标题
 * @param mode 错误消息模型
 */
export function getAppTitle(mode: ErrorMessageMode = 'message') {
  return httpClient.get(
    {
      url: Api.GetAppTitle,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 设置应用标题
/**
 * 设置应用标题
 * @param title 应用标题
 * @param mode 错误消息模型
 */
export function setAppTitle(title?: string | null, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.SetAppTitle,
      params: { title: title },
    },
    {
      joinParamsToUrl: true,
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 删除应用标题
/**
 * 删除应用标题
 * @param mode 错误消息模型
 */
export function deleteAppTitle(mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.DeleteAppTitle,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 获取应用描述
/**
 * 获取应用描述
 * @param mode 错误消息模型
 */
export function getAppDescription(mode: ErrorMessageMode = 'message') {
  return httpClient.get<SiteDescription>(
    {
      url: Api.GetAppDescription,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 设置应用描述
/**
 * 设置应用描述
 * @param description 网站描述
 * @param mode 错误消息模型
 */
export function setAppDescription(
  description: SiteDescription,
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<AjaxResult>(
    {
      url: Api.SetAppDescription,
      params: description,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 删除应用描述
/**
 * 删除应用描述
 * @param mode 错误消息模型
 */
export function deleteAppDescription(mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.DeleteAppDescription,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
